﻿/*****************************************************************************
* File			: src.Game
* Purpose		: Contains Declaration and Implementation for main class : Game
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.events.*
	import flash.display.MovieClip;
	import flash.display.*
	
	/**************************************
	* Class		: Game
	* Purpose	: Logic for main class : Game
	* Parent	: MovieClip
	**************************************/
	public class Game extends MovieClip
	{
		// Class variables
		public static var WIDTH			:int = 595; // Game backgournd width
		public static var HEIGHT		:int = 300; // Game backgournd height
		public static var FPS			:int = 60;	// Animation framerate
		
 		public static var main			:Game;		// static reference to the only game instance
		public 		  var ship			:Ship;  	// reference tot he current Hero ship
		
		private 	  var nCurrentScore	:int = 0;	// Current score	
		
		//Constructor
		public function Game()
		{ 
			//Init main to current instance
			main = this;
			
			// Initialize Key input management class
			Key.initialize(stage);
			
			// Add listener for the play button
			splashScreen.playButton.addEventListener(MouseEvent.CLICK, onPlay);
			
			// Hide EnemyHPbar
			enemyHPMeter.visible = false;
			
			// Hide Gameover menu
			gameOverMenu.visible = false;
			gameWinMenu.visible = false;
		}

		/***********************************************************
		* Method		: newGame
		* Purpose		: Launch game again
		* Parameters	: None
		* return		: void
		***********************************************************/
		function newGame()
		{
			// Hide GameOver menu
			gameOverMenu.visible = false;
			gameWinMenu.visible = false;
			
			//start the level manager that makes timers for power up intervals, enemy ship intervals, and miniboss intervals, etc...
			LevelManager.init();
			
			//make a new hero ship and position it in the middle of the stage			
			ship = new Ship();
			
			//display the new hero ship
			spriteClip.addChild(ship);

			// Reset score
			resetScore();
		}

		/***********************************************************
		* Method		: gameOver
		* Purpose		: end game by loosing
		* Parameters	: None
		* return		: void
		***********************************************************/
		function gameOver()
		{
			// Delete ship
			if (Game.main.ship != null)
			{
				Game.main.ship.onDelete();
			}
			
			//stop all the imers that level mamager started
			LevelManager.main.stop();
			
			// Show gameover menu
			Game.main.gameOverMenu.visible = true;
			
			// add listeners for PlayAgain button
			gameOverMenu.playAgainButton.addEventListener(MouseEvent.CLICK, onPlayAgain);
		}

		/***********************************************************
		* Method		: onPlay
		* Purpose		: Fired when Play button is clicked
		* Parameters	: e:MouseEvent
		* return		: void
		***********************************************************/
		public function onPlay(e:MouseEvent)
		{
			//remove listener
			gameOverMenu.playAgainButton.removeEventListener(MouseEvent.CLICK, onPlay);
			gameWinMenu.playAgainButton.removeEventListener(MouseEvent.CLICK, onPlay);
			
			// Hide splashscreen
			splashScreen.visible = false;
			
			// And start again
			newGame();
		}
		
		/***********************************************************
		* Method		: onPlayAgain
		* Purpose		: Fired when Play over button is clicked
		* Parameters	: e:MouseEvent
		* return		: void
		***********************************************************/
		public function onPlayAgain(e:MouseEvent)
		{
			//remove listener
			gameOverMenu.playAgainButton.removeEventListener(MouseEvent.CLICK, onPlayAgain);
			
			// And start again
			newGame();
		}
		
		/***********************************************************
		* Method		: resetScore
		* Purpose		: Reset the score and update UI
		* Parameters	: None
		* return		: void
		***********************************************************/
		function resetScore()
		{
			nCurrentScore = 0;
			scoreText.text = String(nCurrentScore);
		}

		/***********************************************************
		* Method		: updateScore
		* Purpose		: Manage the score update
		* Parameters	: nPoints:int : the number of poitns we want to add
		* return		: void
		***********************************************************/
		function updateScore(nPoints:int)
		{
			nCurrentScore += nPoints;
			scoreText.text = String(nCurrentScore);
		}
	}
}